<?php
namespace App\Models;
use DB;
use Exception;
use Mail;

/**
 * 会员咨询类
 */
class MFeedback extends BaseModel
{
    public $table = "feedback";

    //数据验证区===========================================================================
    /**
     * 信息存储验证
     * $flag 0 = rules  1 = msg
     */
    public static function editRulesWithMsg($flag)
    {
        $rules = array(
            'content' => 'required',
        );

        $msg = array(
            "content.required" => "请填写回复内容！"
        );

        if ($flag == 0) {
            return $rules;
        } else {
            return $msg;
        }
    }

    //数据逻辑区===========================================================================

    public static function sendMessage($content)
    {
        $send = new MFeedback();
        $send->uid = session('uid');
        $send->uname = session('uname');
        $send->content = $content;
        $send->time = time();
        $send->status = 0;

        return $send->save();
    }

    /**
     * 管理员回复咨询
     */
    public function saveData($request)
    {

        $flag = Mail::raw(strip_tags($request->content),function($message){
            $message->subject('金世源家居厂回复邮件');
            $message->to($this->email);
        });

        if($flag){
            $this->reply = $request->content;
            $this->reply_time = time();
            $this->status = 1;
            return $this->save();

        }else{
            return false;
        }

    }


    /**
     * 友客咨询保存
     */
    public function saveFeed ($request,$feed)
    {
        $feed->name = $request->uname;
        $feed->email = $request->email;
        $feed->addr = $request->addr;
        $feed->content = $request->content;
        $feed->time  =time();
        return $feed->save();
    }

}